package com.youlai.eng.controller;

import com.youlai.eng.entity.EngBill2Entity;
import com.youlai.eng.query.PageQuery;
import com.youlai.eng.service.EngBill2Service;
import com.youlai.common.result.PageResult;
import com.youlai.common.result.Result;
import com.youlai.utils.IdWorker;
import com.youlai.common.plugin.dupsubmit.annotation.PreventDuplicateSubmit;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

@Tag(name = "工程员工管理接口 ")
@RestController
@RequestMapping("/api/v1/engbill2")
@RequiredArgsConstructor
public class EngBill2Controller {

    private final EngBill2Service service;

    @Operation(summary = "分页列表")
    @GetMapping("/page")
    public PageResult<EngBill2Entity> getPage(@ParameterObject PageQuery queryParams) {
        PageResult<EngBill2Entity> page = service.getPageByHql(queryParams);
        return page;
    }

    @Operation(summary = "获取表单数据")
    @GetMapping("/{id}/form")
    public Result<EngBill2Entity> getForm(
            @Parameter(description ="字典ID") @PathVariable Long id
    ) {
        EngBill2Entity entity = service.findById(id);
        return Result.success(entity);
    }

    @Operation(summary = "新增")
    @PostMapping
    @PreAuthorize("@ss.hasPerm('eng:engbill2:add')")
    @PreventDuplicateSubmit
    public Result insert(@RequestBody EngBill2Entity form) {
        form.setEngBill2Id(IdWorker.generateId());
        service.save(form);
        return Result.judge(true);
    }

    @Operation(summary = "修改")
    @PutMapping("/{id}")
    @PreAuthorize("@ss.hasPerm('eng:engbill2:edit')")
    public Result update(@PathVariable Long id, @RequestBody EngBill2Entity form) {
        form.setEngBill2Id(id);

        service.update(form);
        return Result.judge(true);
    }

    @Operation(summary = "删除")
    @DeleteMapping("/{ids}")
    @PreAuthorize("@ss.hasPerm('eng:engbill2:del')")
    public Result deleteByIds(@Parameter(description ="主键ID，多个以英文逗号(,)拼接") @PathVariable String ids) {
        boolean result = service.deleteAllById(ids);
        return Result.judge(result);
    }

}
